package cn.maihe.elg.operation.supports.electronicseal.anne.service.impl;

import cn.maihe.elg.operation.config.AnneSignConfig;
import cn.maihe.elg.operation.config.ElgConfig;
import cn.maihe.elg.operation.supports.electronicseal.anne.AnneServiceName;
import cn.maihe.elg.operation.supports.electronicseal.anne.BaseAnneSignService;
import cn.maihe.elg.operation.supports.electronicseal.anne.EnterpriseStatus;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.enterprise.EnterpriseQueryRequest;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.enterprise.EnterpriseQueryRequestBody;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.enterprise.EnterpriseQueryResponse;
import cn.maihe.elg.operation.supports.electronicseal.anne.bean.annesign.enterprise.vo.EnterpriseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Description  企业创建结果查询
 * @Author WJH
 * @Date 2021/08/13
 */
@Slf4j
@Service
public class EnterpriseResultQueryService extends BaseAnneSignService<EnterpriseQueryRequest, EnterpriseQueryResponse> {

    public EnterpriseResultQueryService(AnneSignConfig anneSignConfig, ElgConfig elgConfig) {
        super(anneSignConfig, elgConfig);
    }

    @Override
    protected AnneServiceName serviceName() {
        return AnneServiceName.enterprise_query;
    }

    /**
     * 企业状态，0：待审核，5：审核失败，9：审核通过（详见数据字典：auditStatus）
     * 为null则标识查无此 信用代码企业，可用创建
     */
    public EnterpriseResult queryEnterpriseResult(String creditCode) {
        EnterpriseQueryRequest request = this.buildBaseReq();
        EnterpriseQueryRequestBody body = new EnterpriseQueryRequestBody();
        body.setLicenseCode(creditCode);
        request.setBody(body);

        EnterpriseQueryResponse response = this.doPost(request);
        if (isNotExistsEnterprise(response) || response.getBody() == null) {
            return EnterpriseResult.of(EnterpriseStatus.NOT_EXISTS, response.getDetail(), null);
        }
        /**企业状态，0：待审核，5：审核失败，9：审核通过（详见数据字典：auditStatus）*/
        String auditStatus = response.getBody().getStatus();
        String auditOpinion = response.getBody().getAuditOpinion();
        Long enterpriseId = response.getBody().getEnterpriseId();
        if ("0".equals(auditStatus)) {
            return EnterpriseResult.of(EnterpriseStatus.AUDITING, auditOpinion, enterpriseId);
        }
        if ("5".equals(auditStatus)) {
            return EnterpriseResult.of(EnterpriseStatus.AUDIT_REJECT, auditOpinion, enterpriseId);
        }
        if ("9".equals(auditStatus)) {
            return EnterpriseResult.of(EnterpriseStatus.AUDIT_PASS, auditOpinion, enterpriseId);
        }

        //默认企业不存在
        return EnterpriseResult.of(EnterpriseStatus.NOT_EXISTS, response.getDetail(), enterpriseId);

    }


}
